import sys, os, time, re

def getExt(nameFile):
    if re.match(r".+jpg\b",nameFile):
        extFile = "jpg"
    if re.match(r".+jpeg\b",nameFile):
        extFile = "jpeg"
    return extFile
    

if __name__ == '__main__':

    newFilePrefix = sys.argv[1]
    
    currentPath = os.path.abspath(os.path.curdir)

    listFiles = os.listdir(currentPath)

    dictFiles = {}
    for aFile in listFiles:
        if re.match(r"\b[A-Za-z0-9]+\.jpe?g\b",aFile):
            dictFiles[aFile] = os.path.getmtime(aFile)
    
    tupleFiles = sorted(dictFiles.items(),lambda x, y: cmp(x[1],y[1]))

    count = 1
    for tupleFile in tupleFiles:
        os.rename(tupleFile[0],"%s%04i.%s"% (newFilePrefix,count,getExt(tupleFile[0])))
        count = count + 1

    print "%i docs' names have been modified!"%(count-1)   

